﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：人员管理
 */
var selectedRow;
var refreshGirdData;
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var departmentId = '';

    var page = {
        init: function () {
            page.inittree();
            page.initGrid();
            page.bind();
            page.search();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });

            // 部门选择
            $('#department_select').lrselect({
                type: 'tree',
                placeholder: '请选择部门',
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        departmentId = '';
                    } else {
                        departmentId = item.value;
                    }
                    page.search();
                }
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!companyId) {
                    learun.alert.warning('请选择公司！');
                    return false;
                }

                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加账号',
                    url: config.webportal_server + 'pages/system/user/form.html?companyId=' + companyId + '&departmentId=' + departmentId + '&access_token=' + config.getToken().access_token,
                    width: 750,
                    height: 420,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑账号',
                        url: config.webportal_server + 'pages/system/user/form.html?companyId=' + companyId + '&departmentId=' + departmentId + '&access_token=' + config.getToken().access_token,
                        width: 750,
                        height: 420,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'users/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 用户数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'users/exportUser?companyId=' + companyId + '&status=1' + '&access_token=' + config.getToken().access_token;
            });
            // 启用
            $('#lr_enabled').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要【启用】账号！', function (res) {
                        if (res) {
                            learun.postForm2(config.base_server() + 'users/' + keyValue + '/updateStatus?access_token=' + config.getToken().access_token, {
                                status: 1
                            }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 禁用
            $('#lr_disabled').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要【禁用】账号！', function (res) {
                        if (res) {
                            learun.postForm2(config.base_server() + 'users/' + keyValue + '/updateStatus?&access_token=' + config.getToken().access_token, {
                                status: 0
                            }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 重置账号
            $('#lr_resetpassword').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要【重置密码】！', function (res) {
                        if (res) {
                            learun.postForm(config.base_server() + 'users/' + keyValue + '/resetPassword?access_token=' + config.getToken().access_token, function () {
                            });
                        }
                    });
                }
            });
            // 功能授权
            $('#lr_authorize').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'authorizeForm',
                        title: '功能授权 - ' + selectedRow.realName,
                        url: config.webportal_server + 'pages/system/authorize/form.html?objectId=' + keyValue + '&objectType=User&access_token=' + config.getToken().access_token,
                        width: 550,
                        height: 690,
                        btn: null
                    });
                }
            });
            // 数据授权
            $('#lr_dataauthorize').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'appAuthorizeForm',
                        title: '数据授权 - ' + selectedRow.realName,
                        url: config.webportal_server + 'pages/system/authorize/appForm.html?objectId=' + keyValue + '&objectType=User&access_token=' + config.getToken().access_token,
                        width: 550,
                        height: 690,
                        callBack: function (id) {
                            return top[id].acceptClick();
                        }
                    });
                }
            });
        },
        inittree: function () {
            $('#companyTree').lrtree({
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                param: {parentId: '0'},
                nodeClick: page.treeNodeClick
            });
            $('#companyTree').lrtreeSet('checkFirst');
        },
        treeNodeClick: function (item) {
            companyId = item.id;
            $('#titleinfo').text(item.text);

            $('#department_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {companyId: companyId, parentId: '0'},
            });
            page.search();
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'users?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '账户', name: 'userName', width: 100, align: 'left'},
                    {label: '姓名', name: 'realName', width: 160, align: 'left'},
                    {
                        label: '性别', name: 'sex', width: 45, align: 'center',
                        formatter: function (cellvalue) {
                            if (cellvalue == "0") {
                                return "女";
                            } else if (cellvalue == "1") {
                                return "男";
                            } else {
                                return "";
                            }
                        }
                    },
                    {label: '手机', name: 'phone', width: 100, align: 'center'},
                    {
                        label: '部门', name: 'departmentId', width: 100, align: 'left',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('department', {
                                key: value,
                                callback: function (item) {
                                    callback(item.name);
                                }
                            });
                        }
                    },
                    {
                        label: "状态", name: "status", index: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            if (cellvalue == 1) {
                                return '<span class=\"label label-success\" style=\"cursor: pointer;\">正常</span>';
                            } else if (cellvalue == 0) {
                                return '<span class=\"label label-default\" style=\"cursor: pointer;\">禁用</span>';
                            }
                        }
                    },
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "备注", name: "description", index: "description", width: 200, align: "left"}

                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑账号',
                            url: config.webportal_server + 'pages/system/user/form.html?companyId=' + companyId + '&access_token=' + config.getToken().access_token,
                            width: 750,
                            height: 420,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.departmentId = departmentId;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        var keyword = $('#txt_Keyword').val();
        page.search({keyword: keyword});
    };

    page.init();
}


